package yang.VirtualMachine.chapter9.classStruct.constantPool;

import java.io.IOException;
import java.io.InputStream;

import yang.VirtualMachine.chapter9.ClassBean;
import yang.VirtualMachine.chapter9.ClassUtil;
import yang.VirtualMachine.chapter9.Processer;

/**
 * 常量池解析
 * @author xinxin
 */
public class ConstantPoolProcesser implements Processer {
	
	private long poolLength = 0;
	
	@Override
	public void start(InputStream input) throws IOException {
		// TODO 怎样调用接口中默认方法
		System.out.println(this.getClass().getSimpleName() + " 开始");
		byte[] dest = new byte[2];
		input.read(dest);
		poolLength = ClassUtil.getNumByBytes(dest);
		ClassBean.getInstance().constantPoolLength = poolLength;
		ClassBean.getInstance().constantPool = new PoolValue[(int) poolLength];
		System.out.println("ConstantsPool长度: " + poolLength);
	}

	@Override
	public void process(InputStream input) throws IOException {
		int i = 1; // 常量池数目为length(第0项常量没有)
		while (i < poolLength) {
			// 每次处理一项常量
			PoolValue value = ConstantPoolController.processController(input);
			ClassBean.getInstance().constantPool[i] = value;
			i++;
		}

	}

}
